package master;

import java.awt.*;
import java.awt.event.*;
import java.util.*;

import javax.swing.*;


public class QuantumRegisterButtonPanel extends JPanel implements ActionListener{
	private final QuantumRegister r;
	private Component myOwner;
	public static int randomiseSeed = 0;//use for debugging
	JButton [] buttons = new JButton[]{
			new JButton("Normalise"),
			new JButton("Collapse"),
			new JButton("Randomize"),
	};
	
	public QuantumRegisterButtonPanel(QuantumRegister parent, Component myOwner){
		r = parent;
		this.myOwner = myOwner;
		for(JButton p : buttons){
			p.addActionListener(this);
			add(p);
		}
	}
	
	public void actionPerformed(ActionEvent arg0) {
		if(arg0.getActionCommand()=="Normalise"){
			r.normalise();
		}else if(arg0.getActionCommand()=="Collapse"){
			r.collapseToEigenstate();
		}else if(arg0.getActionCommand()=="Randomize"){
			Random myRnd;
			if(randomiseSeed!=0)myRnd = new Random(randomiseSeed);
			else myRnd = new Random();
			for(int i = 0 ; i < r.getDimension() ; i++)
				r.setCoefficient(new ComplexNumber(myRnd.nextFloat()-0.5f,myRnd.nextFloat()-0.5f), i);
			r.normalise();
		}
		for(Frame f : JFrame.getFrames())f.repaint();//repaint all frames in program
	}
}
